package ltd.newbee.mall.controller.mall;

import ltd.newbee.mall.common.Constants;
import ltd.newbee.mall.common.NewBeeMallException;
import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.controller.vo.FilmVO;
import ltd.newbee.mall.controller.vo.NewBeeMallGoodsDetailVO;
import ltd.newbee.mall.controller.vo.SearchPageCategoryVO;
import ltd.newbee.mall.entity.Film;
import ltd.newbee.mall.entity.NewBeeMallGoods;
import ltd.newbee.mall.entity.Schedule;
import ltd.newbee.mall.service.FilmService;
import ltd.newbee.mall.service.ScheduleService;
import ltd.newbee.mall.util.BeanUtil;
import ltd.newbee.mall.util.PageQueryUtil;
import ltd.newbee.mall.util.Result;
import ltd.newbee.mall.util.ResultGenerator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/film")
public class FilmController {

    @Resource
    private FilmService filmService;

    @Resource
    private ScheduleService scheduleService;

    //单个电影详情
    @GetMapping("/film")
    public String detailPage(Model model) {
//        List<Film> films=filmService.getFilms();
//        model.addAttribute("films",films);
        return "film/index";
    }

    //单个电影详情
    @GetMapping("/detail/{filmId}")
    public String detailPage(@PathVariable("filmId") int filmId, HttpServletRequest request) {
        if (filmId < 1) {
            return "error/error_5xx";
        }
        Film film = filmService.selectFilm(filmId);
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String time=dateFormat.format(film.getFileReleaseTime());
        if (film == null) {
            NewBeeMallException.fail(ServiceResultEnum.GOODS_NOT_EXIST.getResult());
        }
        if (Constants.SELL_STATUS_UP != film.getFilmSellStatus()) {
            NewBeeMallException.fail(ServiceResultEnum.GOODS_PUT_DOWN.getResult());
        }
        request.setAttribute("filmDetail", film);
        request.setAttribute("time", time);
        return "mall/detail";
    }


    //单个电影详情
    @GetMapping("/selectFilm/{filmId}")
    public String selectFilm(@PathVariable("filmId") int filmId, HttpServletRequest request) {
        if (filmId < 1) {
            return "error/error_5xx";
        }
        //电影详情
        Film film = filmService.selectFilm(filmId);
        //电影时间班次
        List<Schedule> schedules = scheduleService.selectGroupFilm(filmId);
        System.out.println("schedules:"+schedules);
        //电影当日班次
//        List<Schedule> Films = scheduleService.selectFilm(filmId);
//        System.out.println("Films:"+Films);

        //电影

        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String time=dateFormat.format(film.getFileReleaseTime());
        if (film == null) {
            NewBeeMallException.fail(ServiceResultEnum.GOODS_NOT_EXIST.getResult());
        }
        if (Constants.SELL_STATUS_UP != film.getFilmSellStatus()) {
            NewBeeMallException.fail(ServiceResultEnum.GOODS_PUT_DOWN.getResult());
        }
        request.setAttribute("filmDetail", film);
        request.setAttribute("time", time);
        return "mall/index";
    }


}
